package com.example.ch01;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private TextView tv_sex_show;
    private TextView tv_hobby_show;
    private ListView lv;
    private String s="";
    private List<String> strs;
    private Button btn_login,btn_regist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_sex_show=(TextView)findViewById(R.id.tv_sex_show);
        tv_hobby_show=(TextView)findViewById(R.id.tv_bobby_show);
        btn_login=(Button)findViewById(R.id.btn_login);
        btn_regist=(Button)findViewById(R.id.btn_regist);
        btn_login.setOnClickListener(this);
        btn_regist.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_login:
                Toast.makeText(this,"登陆中...",Toast.LENGTH_SHORT).show();
                break;
            case R.id.btn_regist:
                Toast.makeText(this,"已取消",Toast.LENGTH_SHORT).show();
                break;
        }
    }

    //定义登陆后的选项
    public void choose_sex(View view){
        AlertDialog alertDialog = new AlertDialog
                .Builder(MainActivity.this)
                .setTitle("请选择性别：")
                .setIcon(R.drawable.sex)
                .setSingleChoiceItems(new String[]{"男", "女"},-1, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
//                        Toast.makeText(MainActivity.this,"选择了第"+which+"个",Toast.LENGTH_SHORT).show();
                            switch (which){
                                case 1:
                                    tv_sex_show.setText("女");
                                    break;
                                case 0:
                                    tv_sex_show.setText("男");
                                    break;
                            }
                    }
                })
                .setPositiveButton("确定",null)
                .show();

        }
    public void choose_hobby(View view){
        String str[]={"旅游", "美食", "睡觉", "玩", "追剧"};
        final boolean[] checkitems=new boolean[]{false,false,false,false,false};
        new AlertDialog.Builder(this)
                .setTitle("请选择你的兴趣爱好！")
                .setIcon(R.drawable.ho)
                .setMultiChoiceItems(new String[]{"旅游", "美食", "睡觉", "玩", "追剧"}, null, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        strs=new ArrayList<>();
                        checkitems[which]=isChecked;
                        for(int i=0;i<str.length;i++){
                            if(checkitems[i]){
                                strs.add(0,str[i]);
                            }
                            s= TextUtils.join(",",strs);
                        }

                        tv_hobby_show.setText(s);
                    }

                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        tv_hobby_show.setText(s);
                        s=null;
                    }
                })
                .setNegativeButton("取消",null)
                .show();

    }

}